package com.ruikong.dazhi.controller.basicinfo;

import com.github.pagehelper.PageInfo;

import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.controller.result.ResultApi;
import com.ruikong.dazhi.entity.basicinfo.Line;
import com.ruikong.dazhi.entity.basicinfo.Shop;
import com.ruikong.dazhi.model.input.LinePageInput;
import com.ruikong.dazhi.model.output.LineOutput;
import com.ruikong.dazhi.model.output.PersonnelOutput;
import com.ruikong.dazhi.model.output.ShopLineOutput;
import com.ruikong.dazhi.service.basicinfo.ILineService;
import com.ruikong.dazhi.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * (Line)表控制层
 *
 * @author Hassion.Tang
 * @since 2020-07-01 15:00:44
 */
@RestController
@RequestMapping("/data")
@EnableAutoConfiguration
@Api(tags = "产线信息管理")
public class LineController {

    private static final Logger logger = LoggerFactory.getLogger(LineController.class);
    /**
     * 产线信息服务对象
     */
    @Autowired
    private ILineService lineService;

    @PostMapping("/getLineById")
    @ApiOperation("根据id获取产线信息")
    public Line getLineById(@RequestBody Line line) {
        return lineService.queryById(line.getId());
    }

    /**
     * Description: 产线信息分页
     *
     * @Param: [baseInputPage]
     * @Return: com.github.pagehelper.PageInfo<com.ruikong.cpisystem.model.Line>
     * @Author: Hassan.Tang
     * @Date: 2020/7/1 15:49
     **/

    @PostMapping("/getLinePageList")
    @ApiOperation(value = "产线信息分页")
    public PageInfo<Line> getLinePageList(@RequestBody LinePageInput input) {
        PageInfo<Line> pageInfo = new PageInfo<>();
        try {
            pageInfo = lineService.getLinePageInfo(input);
        } catch (Exception e) {
            logger.error("getLinePageList{}", e.getMessage());
            e.printStackTrace();
        }
        return pageInfo;

    }

    @PostMapping("/editLine")
    @ApiOperation("新增或修改产线信息")
    public BaseResult editLine(HttpSession session, HttpServletRequest request, HttpServletResponse response, @RequestBody Line line) {
        BaseResult result = new BaseResult();
        try {
            Object object = session.getAttribute("outPersonnel");
            if (object == null) {
                response.sendRedirect(request.getContextPath() + "/view/login");
            }
            PersonnelOutput personnel = (PersonnelOutput) object;

            if (line.getId() != 0) {
                if (lineService.isExist(line) > 0) {
                    result.setCode(ResultApi.failCode);
                    result.setMsg("该产线编号已经存在!");
                    return result;
                }
                int effect = lineService.update(line);
                if (effect > 0) {
                    result.setCode(ResultApi.successCode);
                    result.setMsg(ResultApi.updateSuccessMsg);
                } else {
                    result.setCode(ResultApi.failCode);
                    result.setMsg(ResultApi.updateFailMsg);
                }
            } else {
                if (lineService.isExist(line) > 0) {
                    result.setCode(ResultApi.failCode);
                    result.setMsg("该产线编号已经存在!");
                    return result;
                }
                line.setCreate_time(new Date());
                line.setUser_id(personnel.getPersonnelId());
                line.setIs_deleted(0);
                line.setState(1);
                int effect = lineService.insert(line);
                if (effect > 0) {
                    result.setCode(ResultApi.successCode);
                    result.setMsg(ResultApi.addSuccessMsg);
                } else {
                    result.setCode(ResultApi.failCode);
                    result.setMsg(ResultApi.addFailMsg);
                }
            }

        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.addFailMsg);
            logger.error("编辑产线出错：{}", e.getMessage());
        }
        return result;
    }

    /**
     * Description: 新增产线信息
     *
     * @Param: [line]
     * @Return: com.ruikong.cpisystem.controller.baseResult.PlatformResult
     * @Author: Hassan.Tang
     * @Date: 2020/7/1 15:36
     **/
    @PostMapping("/addLine")
    @ApiOperation("新增产线信息")
    public BaseResult addLine(@RequestBody Line line) {
        BaseResult result = new BaseResult();
        try {
            if (lineService.isExist(line) > 0) {
                result.setCode(ResultApi.failCode);
                result.setMsg("该产线编号已经存在!");
                return result;
            }
            int effect = lineService.insert(line);
            if (effect > 0) {
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.addSuccessMsg);
            } else {
                result.setCode(ResultApi.failCode);
                result.setMsg(ResultApi.addFailMsg);
            }
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.addFailMsg);
            logger.error("addLine{}", e.getMessage());
        }
        return result;
    }


    /**
     * Description: 更新产线信息
     *
     * @Param: [line]
     * @Return: com.ruikong.cpisystem.controller.baseResult.PlatformResult
     * @Author: Hassan.Tang
     * @Date: 2020/7/1 15:35
     **/
    @PostMapping("/updateLine")
    @ApiOperation("更新产线信息")
    public BaseResult updateLine(@RequestBody Line line) {
        BaseResult platformResult = new BaseResult();
        try {
            if (lineService.isExist(line) > 0) {
                platformResult.setCode(ResultApi.failCode);
                platformResult.setMsg("该产线编号已经存在!");
                return platformResult;
            }
            int effect = lineService.update(line);
            if (effect > 0) {
                platformResult.setCode(ResultApi.successCode);
                platformResult.setMsg(ResultApi.updateSuccessMsg);
            } else {
                platformResult.setCode(ResultApi.failCode);
                platformResult.setMsg(ResultApi.updateFailMsg);
            }
        } catch (Exception e) {
            platformResult.setCode(ResultApi.failCode);
            platformResult.setMsg(ResultApi.updateFailMsg);
            logger.error("updateLine{}", e.getMessage());

        }
        return platformResult;
    }


    /**
     * Description: 删除产线信息
     *
     * @Param: [line]
     * @Return: com.ruikong.cpisystem.controller.baseResult.PlatformResult
     * @Author: Hassan.Tang
     * @Date: 2020/7/1 15:44
     **/
    @PostMapping("deleteLine")
    @ApiOperation(value = "删除产线信息")
    public BaseResult deleteLine(@RequestBody Line line) {
        BaseResult platformResult = new BaseResult();
        try {
            boolean result = lineService.deleteById(line.getId());
            if (result) {
                platformResult.setMsg(ResultApi.deleteSuccessMsg);
                platformResult.setCode(ResultApi.successCode);
            } else {
                platformResult.setMsg(ResultApi.deleteFailMsg);
                platformResult.setCode(ResultApi.failCode);
            }
        } catch (Exception e) {
            platformResult.setMsg(ResultApi.deleteFailMsg);
            platformResult.setCode(ResultApi.failCode);
            logger.error("deleteLine{}", e.getMessage());
        }
        return platformResult;
    }

    @PostMapping("/getLineList")
    @ApiOperation(value = "获取系统所有产线信息")
    public BaseResult getLineList() {
        BaseResult platformResult = new BaseResult();
        try {
            List<Line> lineLists = lineService.getLineList();
            platformResult.setCode(ResultApi.successCode);
            platformResult.setData(lineLists);
        } catch (Exception e) {
            platformResult.setCode(ResultApi.failCode);
            logger.error("getLineList{}", e.getMessage());
        }

        return platformResult;
    }

    @PostMapping("/getShopLineTree")
    @ApiOperation("绑定车间产线树")
    public BaseResult getShopLineTree() {
        BaseResult result = new BaseResult();
        try {
            List<ShopLineOutput> list = lineService.getShopLineTree();
            if (list.size() == 0) {
                result.setCode(ResultApi.failCode);
                result.setMsg(ResultApi.getFailMsg);
            } else {
                result.setCode(ResultApi.successCode);
                result.setMsg(ResultApi.getSuccessMsg);
                result.setData(list);
            }
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
            logger.error("绑定包材类别树出错：{}", e.getMessage());
        }
        return result;
    }

    @PostMapping("/getLineListByShop")
    @ApiOperation("根据车间获取产线")
    public BaseResult getLineListByShop(@RequestBody Shop shop) {
        BaseResult result = new BaseResult();
        try {
            List<Line> list = lineService.getLineListByShop(shop.getId());
            result.setCode(ResultApi.successCode);
            result.setMsg(ResultApi.getSuccessMsg);
            result.setData(list);

        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.getFailMsg);
            logger.error("根据车间获取产线出错：{}", e.getMessage());
        }
        return result;
    }


    @PostMapping("/getLineByLineType")
    @ApiOperation("根据产线类型获取产线信息")
    public BaseResult getLineByLineType(@RequestBody Line line) {
        BaseResult baseResult = new BaseResult();
        try {
            List<Line> lineList = this.lineService.getLineByLineType(line.getLine_type());
            baseResult.setMsg(ResultApi.getSuccessMsg);
            baseResult.setCode(ResultApi.successCode);
            baseResult.setData(lineList);
        } catch (Exception e) {
            baseResult.setMsg(ResultApi.getFailMsg);
            baseResult.setCode(ResultApi.failCode);
            logger.error("getLineByLineType{}", e.getMessage());
        }
        return baseResult;
    }

    @PostMapping("/getPlanNumberByLine")
    @ApiOperation("获取配制工单下的产线情况")
    public BaseResult getPlanNumberByLine() {
        BaseResult result = new BaseResult();
        try {
            List<LineOutput> lineOutputList = lineService.getPackageOrderNumberByLine();
            result.setData(lineOutputList);
            result.setCode(ResultApi.successCode);
        } catch (Exception e) {
            result.setCode(ResultApi.failCode);
            result.setMsg(ResultApi.deleteFailMsg);
            logger.error("获取配制工单下的产线情况：{}", e.getMessage());
        }
        return result;
    }

    @GetMapping("/downLineExcelTemplate")
    @ApiOperation("车间导入模版下载")
    public void downLineExcelTemplate(HttpServletRequest request, HttpServletResponse response, @RequestParam("fileName") String fileName) {
        String[] columNames = {"产线编号","产线名称", "所属车间", "产线描述", "创建时间","创建人", "备注"};
        ExcelUtil.download(response, request, fileName, columNames);
    }

    @PostMapping("/lineImport")
    @ApiOperation("车间信息导入")
    public BaseResult lineImport( @RequestParam("file") MultipartFile multipartFile, HttpSession httpSession){
        PersonnelOutput personnelOutput = (PersonnelOutput) httpSession.getAttribute("outPersonnel");
        int personnelId = personnelOutput.getPersonnelId();
        BaseResult baseResult=new BaseResult();
        try {
            baseResult=this.lineService.lineImport(multipartFile,personnelId);
        }catch (Exception e){
            logger.error("lineImport",e.getMessage());
            e.printStackTrace();
        }
        return baseResult;
    }

}